/*
 * Copyright (C) 2003, Velocityme Partnership
 * SearchLevelBean.java
 *
 * Created on 21 July 2003, 08:50
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="SearchLevel"
 *           jndi-name="ejb/SearchLevel"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.persistence table-name="searchlevel"
 *
 * @ejb.util generate="physical"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="true"
 */
public abstract class SearchLevelBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
          
    /** @ejb.create-method */
    public com.velocityme.interfaces.SearchLevelPK ejbCreate(com.velocityme.interfaces.SearchLocal p_search,
                                                             java.lang.Integer p_level,
                                                             java.util.Collection p_nodesLocal) throws CreateException {
        try {
            setSearchLevelId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.SearchLocal p_search,
                              java.lang.Integer p_level,
                              java.util.Collection p_nodesLocal) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setSearchLocal(p_search);
        setLevel(p_level);
        setNodesLocal(p_nodesLocal);
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("searchlevel","searchLevelId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.SearchLevelLocal getLocal() {
        return (SearchLevelLocal)m_entityContext.getEJBLocalObject();
    }
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="searchLevelId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getSearchLevelId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setSearchLevelId(java.lang.Integer searchId);

    /**
     * @ejb.persistence column-name="level"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.Integer getLevel();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLevel(java.lang.Integer level);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Search-SearchLevel"
     *               role-name="SearchLevel-has-a-Search"
     *               cascade-delete="yes"
     * @jboss.relation fk-column="searchIdFk"
     *                 related-pk-field="searchId"
     */
    public abstract com.velocityme.interfaces.SearchLocal getSearchLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setSearchLocal(com.velocityme.interfaces.SearchLocal searchLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="SearchLevel-Node"
     *               role-name="SearchLevel-has-many-Nodes"
//     *               target-ejb="Node"
//     *               target-role-name="Node-has-many-SearchLevels"
//     *               target-multiple="yes"
     * @jboss.relation fk-column="searchLevelId"
     *                 related-pk-field="nodeId"
     * @jboss.relation-table table-name="searchlevelnode"
     *                       create-table="true"
     *                       remove-table="true"
     */
    public abstract java.util.Collection getNodesLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setNodesLocal(java.util.Collection nodes);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
